package cn.ac.iscas.controller;

import cn.ac.iscas.domain.response.Response;
import cn.ac.iscas.domain.response.middleplatform.ResponseEntity;
import cn.ac.iscas.service.sql.SqlService;
import com.iscas.datasong.lib.common.DataSongException;
import com.iscas.datasong.lib.common.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/5/26 8:40
 */
//@CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
@RestController
@RequestMapping({"/sql"})
public class SqlExecuteController {

    @Autowired
    SqlService sqlService;

//    @Autowired
//    IComponentMetadataMapper IComponentMetadataMapper;

    @RequestMapping(value = "/run", method = RequestMethod.POST)
    public Response runSql(@RequestBody String data) {
        return sqlService.executeSql(data);
    }

    /*
     * 多任务批量执行
     */
    @RequestMapping(value = "/run/batch", method = RequestMethod.POST)
    public ResponseEntity executeJob(@RequestBody List<String> jobIds) throws DataSongException {
        ResponseEntity responseEntity = new ResponseEntity(Status.OK.getValue(),"batch execute success");
        try{
            if(sqlService.executeBatchTasks(jobIds)) {
                responseEntity.setData(jobIds);
                responseEntity.setMessage("执行成功");
            }else{
                responseEntity.setMessage("执行失败");
            }
        }catch (DataSongException e){
            e.printStackTrace();
            throw e;
        }catch (Exception e){
            e.printStackTrace();
            throw new DataSongException("执行异常！",e);
        }
        return responseEntity;
    }
}
